package com.ruoyi.web.controller.module.employee;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.employee.domain.EmployeeStatic;
import com.ruoyi.employee.service.IEmployeeStaticService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 店员统计信息
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/employee/static")
public class EmployeeStaticController extends BaseController
{
    @Autowired
    private IEmployeeStaticService employeeStaticService;

    /**
     * 获取店员统计列表
     */
    @PostMapping("/employeeStaticList")
    public TableDataInfo list(@RequestBody EmployeeStatic employeeStatic, HttpServletRequest request)
    {
        String shopId = request.getHeader("ShopId"); // 店铺id
        if(shopId != null && !shopId.equals("")){
            employeeStatic.setShopId(shopId);
        }
        startPageByPost();
        List<EmployeeStatic> list = employeeStaticService.selectUserList(employeeStatic);
        return getDataTable(list);
    }

    /**
     * 获取用户信息
     *
     * @return 用户信息
     */
    @GetMapping("/employeeStaticInfo")
    public AjaxResult employeeStaticInfo()
    {
        AjaxResult ajax = AjaxResult.success();
        Long userId = SecurityUtils.getUserId();
        EmployeeStatic employeeStatic =  employeeStaticService.selectUserById(userId);
        ajax.put(AjaxResult.DATA_TAG, employeeStatic);
        return ajax;
    }

    /**
     * 根据店员统计编号获取详细信息
     */
    @GetMapping(value = { "/", "/{userId}" })
    public AjaxResult getInfo(@PathVariable(value = "userId", required = false) Long userId)
    {
        AjaxResult ajax = AjaxResult.success();
        EmployeeStatic employeeStatic = employeeStaticService.selectUserById(userId);
        ajax.put(AjaxResult.DATA_TAG, employeeStatic);
        return ajax;
    }

    /**
     * 修改店员统计
     */
    @PreAuthorize("@ss.hasPermi('employee:static:edit')")
    @Log(title = "店员统计管理 - 修改", businessType = BusinessType.UPDATE)
    @PostMapping("/employeeStaticUpdate")
    public AjaxResult edit(@Validated @RequestBody EmployeeStatic user)
    {
        EmployeeStatic es = new EmployeeStatic();
        es.setRatio(user.getRatio());
        es.setUserId(user.getUserId());
        es.setUpdateBy(getUsername());
        return toAjax(employeeStaticService.updateUser(es));
    }

    @Log(title = "店员统计管理 - 导出", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('employee:static:export')")
    @PostMapping("/employeeStaticExport")
    public void export(HttpServletResponse response, EmployeeStatic user)
    {
        List<EmployeeStatic> list = employeeStaticService.selectUserList(user);
        ExcelUtil<EmployeeStatic> util = new ExcelUtil<EmployeeStatic>(EmployeeStatic.class);
        util.exportExcel(response, list, "店员统计数据");
    }
}
